package controller.action;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class DataParser {
	public static void NewsData() {
		try {
			Document doc = Jsoup.connect("http://news.naver.com/").get();
			Document data = new Document("");
			data.append(doc.getElementsByClass("type16").toString());
			for (int i = 0; i < data.getElementsByTag("a").size(); i++) {
				data.getElementsByTag("a").get(i).attr("onclick","openContent('http://news.naver.com"+ data.getElementsByTag("a").get(i).attr("href") + "');");
				data.getElementsByTag("a").get(i).removeAttr("href");
			}
			FileWriter out01 = new FileWriter(
					"E:\\9.NHN SM\\1.workplace\\130523_final\\ux_naver\\WebContent\\pasingData\\police.data");
			FileWriter out02 = new FileWriter(
					"E:\\9.NHN SM\\1.workplace\\130523_final\\ux_naver\\WebContent\\pasingData\\economy.data");
			FileWriter out03 = new FileWriter(
					"E:\\9.NHN SM\\1.workplace\\130523_final\\ux_naver\\WebContent\\pasingData\\social.data");
			FileWriter out04 = new FileWriter(
					"E:\\9.NHN SM\\1.workplace\\130523_final\\ux_naver\\WebContent\\pasingData\\culture.data");
			FileWriter out05 = new FileWriter(
					"E:\\9.NHN SM\\1.workplace\\130523_final\\ux_naver\\WebContent\\pasingData\\world.data");
			FileWriter out06 = new FileWriter(
					"E:\\9.NHN SM\\1.workplace\\130523_final\\ux_naver\\WebContent\\pasingData\\it.data");
			FileWriter out07 = new FileWriter(
					"E:\\9.NHN SM\\1.workplace\\130523_final\\ux_naver\\WebContent\\pasingData\\entertain.data");
			FileWriter out08 = new FileWriter(
					"E:\\9.NHN SM\\1.workplace\\130523_final\\ux_naver\\WebContent\\pasingData\\sports.data");

			out01.write(data.getElementsByClass("type16").get(0)
					.attr("id", "police").toString());
			out02.write(data.getElementsByClass("type16").get(1)
					.attr("id", "economy").toString());
			out03.write(data.getElementsByClass("type16").get(2)
					.attr("id", "social").toString());
			out04.write(data.getElementsByClass("type16").get(3)
					.attr("id", "culture").toString());
			out05.write(data.getElementsByClass("type16").get(4)
					.attr("id", "world").toString());
			out06.write(data.getElementsByClass("type16").get(5)
					.attr("id", "it").toString());
			out07.write(data.getElementsByClass("type16").get(6)
					.attr("id", "entertain").toString());
			out08.write(data.getElementsByClass("type16").get(7)
					.attr("id", "sports").toString());

			out01.close();
			out02.close();
			out03.close();
			out04.close();
			out05.close();
			out06.close();
			out07.close();
			out08.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void RealTimeData() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		ArrayList<String> arrayList = new ArrayList<String>();
		try {
			Document doc = Jsoup.connect("http://m.naver.com").get();
			String[] data = doc.getElementById("recommend_json").toString()
					.split("\n");
			FileWriter out = new FileWriter(
					"E:\\9.NHN SM\\1.workplace\\130523_final\\ux_naver\\WebContent\\pasingData\\realtime.json");
			for (String result : data) {
				if (result.contains("try")) {
					arrayList.add(result.split("=")[1].split("; }")[0]);
					System.out.println(result.split("=")[1].split("; }")[0]);
				}
			}
			JSONArray Jsonarray = JSONArray.fromObject(arrayList);
			map.put("alldata", Jsonarray);
			JSONObject jsonObject = JSONObject.fromObject(map);
			out.write(jsonObject.toString());
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void WeatherData() {
		try {
			Document doc = Jsoup.connect("http://m.naver.com/").get();

			for (int i = 0; i < doc.getElementsByClass("toif2_l").size(); i++) {
				doc.getElementsByClass("toif2_l").get(i).removeAttr("style");
			}

			FileWriter out = new FileWriter(
					"E:\\9.NHN SM\\1.workplace\\130523_final\\ux_naver\\WebContent\\pasingData\\weather.data");
			out.write(doc.getElementsByClass("toif2_l").get(5).toString());
			out.write("\n");
			out.write(doc.getElementsByClass("toif2_l").get(6).toString());
			out.write("\n");
			out.write(doc.getElementsByClass("toif2_l").get(7).toString());
			out.write("\n");
			out.write(doc.getElementsByClass("toif2_l").get(8).toString());
			out.write("\n");
			out.write(doc.getElementsByClass("toif2_l").get(9).toString());
			out.write("\n");
			out.write(doc.getElementsByClass("toif2_l").get(10).toString());
			out.write("\n");
			out.write(doc.getElementsByClass("toif2_l").get(11).toString());
			out.write("\n");
			out.write(doc.getElementsByClass("toif2_l").get(12).toString());
			out.write("\n");
			out.write(doc.getElementsByClass("toif2_l").get(13).toString());
			out.write("\n");
			out.write(doc.getElementsByClass("toif2_l").get(14).toString());
			out.write("\n");
			out.write(doc.getElementsByClass("toif2_l").get(15).toString());
			out.write("\n");
			out.write(doc.getElementsByClass("toif2_l").get(16).toString());
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
